package com.jvyou.framework.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jvyou.common.annotation.Log;
import com.jvyou.common.core.domain.model.PageResult;
import com.jvyou.common.core.domain.model.Result;
import com.jvyou.common.enums.log.BusinessType;
import com.jvyou.common.enums.result.HttpCodeEnum;
import com.jvyou.framework.domain.dto.SysFileDto;
import com.jvyou.framework.domain.entity.SysFile;
import com.jvyou.framework.service.SysFileService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 文件表(SysFile)表控制层
 *
 * @author 橘柚
 * @since 2025-05-29 17:51:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/file")
public class SysFileController {

    private final SysFileService sysFileService;

    /**
     * 统一文件上传服务
     *
     * @param file 原始文件
     * @return 文件信息
     */
    @PostMapping("/upload")
    @Log(title = "统一文件上传服务", businessType = BusinessType.IMPORT)
    public Result<SysFile> upload(@RequestParam("file") MultipartFile file) {
        return Result.success(sysFileService.upload(file));
    }

    @GetMapping("/download/{id}")
    @Log(title = "统一文件下载服务", businessType = BusinessType.EXPORT)
    public void download(@PathVariable("id") Long id, HttpServletResponse response) {
        sysFileService.download(id, response);
    }


    @GetMapping("/downloadByPath")
    @Log(title = "统一文件下载服务", businessType = BusinessType.EXPORT)
    public void downloadByPath(@RequestParam String path, HttpServletResponse response) {
        sysFileService.downloadByPath(path, response);
    }
//

    /**
     * 查询文件表列表
     *
     * @param dto 查询参数
     * @return 文件表
     */
    @GetMapping("/list")
    @SaCheckPermission("sys:sysFile:list")
    @Log(title = "查询文件表列表", businessType = BusinessType.OTHER)
    public Result<List<SysFile>> list(@ModelAttribute SysFileDto dto) {
        return Result.success(sysFileService.listSysFile(dto));
    }

    /**
     * 分页查询文件表列表
     *
     * @param dto 查询参数
     * @return 文件表
     */
    @GetMapping("/page")
    @SaCheckPermission("sys:sysFile:page")
    @Log(title = "分页查询文件表列表", businessType = BusinessType.OTHER)
    public Result<PageResult<SysFile>> page(@ModelAttribute SysFileDto dto) {
        return Result.success(sysFileService.pageSysFile(dto));
    }

    /**
     * 通过主键查询文件表单条
     *
     * @param id 文件表主键
     * @return 文件表
     */
    @GetMapping("/{id}")
    @Log(title = "通过主键查询文件表")
    @SaCheckPermission("sys:sysFile:get")
    public Result<SysFile> selectOne(@PathVariable Long id) {
        return Result.success(sysFileService.getById(id));
    }

    /**
     * 新增文件表
     *
     * @param sysFile 文件表实体对象
     * @return 新增文件表结果
     */
    @PostMapping
    @Log(title = "新增文件表", businessType = BusinessType.INSERT)
    @SaCheckPermission("sys:sysFile:insert")
    public Result<SysFile> insert(@RequestBody SysFile sysFile) {
        boolean success = sysFileService.save(sysFile);
        return success ? Result.success(sysFile) : Result.error(HttpCodeEnum.ADD_FAIL);
    }

    /**
     * 修改文件表
     *
     * @param sysFile 文件表实体对象
     * @return 修改文件表结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("sys:sysFile:update")
    @Log(title = "修改文件表数据", businessType = BusinessType.UPDATE)
    public Result<SysFile> updateById(@RequestBody SysFile sysFile) {
        boolean success = sysFileService.updateById(sysFile);
        return success ? Result.success(sysFile) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除文件表数据
     *
     * @param id 文件表主键
     * @return 删除文件表结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("sys:sysFile:delete")
    @Log(title = "删除文件表", businessType = BusinessType.DELETE)
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(sysFileService.deleteFile(id));
    }

}

